package com.lianxian.xindong.commonres.observer;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import com.hxb.library.utils.LogUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import com.lianxian.xindong.commonres.entity.ResultBaseBean;
import com.lianxian.xindong.commonres.utils.TTypeUtils;
import com.lianxian.xindong.commonsdk.http.Api;

/**
 * @Author :hexingbo
 * @Date :2019/11/21
 * @FileName： HttpResultObserver
 * @Describe :
 */
public abstract class MyHttpDataListObserver<T> extends ErrorHandleSubscriber<ResultBaseBean> {

    public static Gson gson = new GsonBuilder().
            registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
                @Override
                public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
                    if (src == src.longValue())
                        return new JsonPrimitive(src.longValue());
                    return new JsonPrimitive(src);
                }
            }).create();

    public MyHttpDataListObserver(RxErrorHandler rxErrorHandler) {
        super(rxErrorHandler);
    }

    @Override
    public void onNext(ResultBaseBean httpResult) {
        LogUtils.debugInfo("-----hxb--dataList---->" + httpResult.toString());
        switch (httpResult.getCode()) {
            case Api.REQUEST_SUCCESS://请求成功
                if (httpResult.getData() == null) {
                    onResult(new ArrayList<T>());
                } else {
                    if (httpResult.getData() != null) {
                        String strJson = httpResult.getData().toString();
                        if (!TextUtils.isEmpty(strJson)) {
                            if (strJson.indexOf("[") == 0) {
                                try {
                                    String data = gson.toJson(httpResult.getData());
                                    Type typeObj = new TypeToken<ArrayList<JsonObject>>() {
                                    }.getType();
                                    ArrayList<JsonObject> jsonObjects = gson.fromJson(data, typeObj);
                                    List<T> arrayList = new ArrayList<>();
                                    try {
                                        String type = TTypeUtils.getTType(MyHttpDataListObserver.this.getClass());
                                        if (type != null && type.length() > 0) {
                                            Class<T> tClass = (Class<T>) Class.forName(type);
                                            for (JsonObject jsonObject : jsonObjects) {
                                                arrayList.add(gson.fromJson(jsonObject, tClass));
                                            }
                                        }
                                        onResult(arrayList);
                                    } catch (ClassNotFoundException e) {
                                        e.printStackTrace();
                                        onResult(new ArrayList<T>());
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    onResult(new ArrayList<T>());
                                }
                            } else {
                                onResult(new ArrayList<T>());
                            }
                        }
                    } else {
                        onResult(new ArrayList<T>());
                    }

                }
                break;
            case Api.REQUEST_LOGIN_OUT://登录失效
                onError(new Throwable("登录已失效，请重新登录"));
                break;
            default:
                onError(new Throwable(TextUtils.isEmpty(httpResult.getMsg()) ? "未知错误" : httpResult.getMsg()));
                break;
        }
    }

    public abstract void onResult(List<T> result);

    @Override
    public void onError(Throwable t) {
        super.onError(t);
    }
}
